package utfpr.gang5.hermes.controledepresenca.cliente.swt;

import java.rmi.RemoteException;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import utfpr.gang5.hermes.controledepresenca.cliente.RmiServiceFactory;
import utfpr.gang5.hermes.controledepresenca.rmi.AlunoPresenca;

public class MonitorPresencas extends ApplicationWindow {
	private Table table;
	private TableViewer tableViewer;
	private Thread thread;

	/**
	 * Create the application window.
	 */
	public MonitorPresencas() {
		super(null);
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		
		tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setBounds(0, 0, 424, 189);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		createColumns(parent, tableViewer);
		
		tableViewer.setContentProvider(new ContentProvider());
		tableViewer.setLabelProvider(new LabelProvider());
		tableViewer.setInput(new Object());
		
		thread = new Thread() {
			public void run() {
				while (true) {
					getShell().getDisplay().asyncExec(new Runnable() {
						@Override
						public void run() {
							tableViewer.refresh();
						}
					});

					try {
						sleep(1000 * 5);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		};
		thread.start();

		return container;
	}
	
	private void createColumns(final Composite parent, final TableViewer viewer) {
		String[] colls = { "Aluno", "Disciplína atual", "Count", "Presença" };
		int[] bounds = { 100, 100, 100, 100 };

		// First column is for the first name
		TableViewerColumn col = createTableViewerColumn(colls[0], bounds[0], 0);

		// Second column is for the last name
		col = createTableViewerColumn(colls[1], bounds[1], 1);


		// Now the gender
		col = createTableViewerColumn(colls[2], bounds[2], 2);

		// // Now the status married
		col = createTableViewerColumn(colls[3], bounds[3], 3);


	}

	private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(101);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;

	}
	
	@Override
	public boolean close() {
		if (thread != null) {
			if (thread.isAlive())
				thread.stop();
		}
		return super.close();
	}
	
	private class ContentProvider implements IStructuredContentProvider {

		@Override
		public void dispose() {
			
		}

		@Override
		public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
			
		}

		@Override
		public Object[] getElements(Object input) {
			List<AlunoPresenca> presencas;
			try {
				presencas = RmiServiceFactory.getService().monitorarPresencas();
				if (presencas != null)
					return presencas.toArray(new AlunoPresenca[0]);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			return null;
			
		}
		
	}
	
	private class LabelProvider implements ITableLabelProvider {

		@Override
		public void addListener(ILabelProviderListener arg0) {
			
		}

		@Override
		public void dispose() {
			
		}

		@Override
		public boolean isLabelProperty(Object arg0, String arg1) {
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Image getColumnImage(Object arg0, int arg1) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int col) {
			AlunoPresenca pres = (AlunoPresenca) element;
			if (col == 0) 
				return pres.getAluno().getNome();
			else if (col == 1) 
				return pres.getDisciplinaAutal().getNome();
			else if (col == 2) 
				return pres.getCount()+"";
			else if (col == 3)
				return pres.isPresente()+"";
			
			return null;
		}
		
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
	}

	/**
	 * Create the menu manager.
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager.
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		return toolBarManager;
	}

	/**
	 * Create the status line manager.
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		return statusLineManager;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			MonitorPresencas window = new MonitorPresencas();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell.
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("New Application");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
}
